// components/v-fooddiet/v-fooddiet.js
const app = getApp();
const call = require('../../../utils/request.js');
const md5 = require('../../../utils/md5.js');
const utils = require('../../../utils/util.js')
Component({
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      var that = this;
      //计算字段
      const timestamp = utils.dataFormat().time2;
      const deviceCode = app.globalData.mac;
      const sign = md5.hexMD5(`timestamp:${utils.dataFormat()},deviceCode:${app.globalData.mac},ver:v1.0,unilife`);
      this.setData({
        // foodList,
        timestamp,
        deviceCode,
        sign
      }, () => {
        //调用设备食材接口
        that._fridgeFoodRecommend()
        //调用所选食材接口
        // that._searchRecipe()
      })

    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    height:{
      type:Number,
      value:250
    },
    recipenameList:{
      type: String,
      value:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    foodList:[],
    timestamp: '',
    deviceCode: '',
    sign: '',
    foodObj: {},
    recipeList: [],
    recipeDetails:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSubFood() {
      var that = this;
      if (that.properties.recipenameList.length!==0){
        console.log('that.properties.recipenameList', that.properties.recipenameList)
        that.setData({
          foodObj: {
            "fridgeFoodNames": that.properties.recipenameList.split('-')
          }
        }, () => {
          that._searchRecipe()
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '您还未选择任何食材，默认给您推荐当前食材菜谱~',
        })
        that._fridgeFoodRecommend()
      }

    },
    onReCo(){
      var that =this;
      console.log('重新推荐');
      that._fridgeFoodRecommend()
    },
    onDetail(e){
      console.log('打开详情！',e.currentTarget.dataset.recipe)
      this.setData({
        recipeDetails: e.currentTarget.dataset.recipe
      })
    },
    _closeDetail(){
      this.setData({
        recipeDetails:{}
      })
    },
    _fridgeFoodRecommend() {
      var that = this;
      const {
        timestamp,
        deviceCode,
        sign
      } = that.data;
      //触发接口
      const url = `/service/app/v1/recipe/fridgeFoodRecommend`;
      const params = {
        "header": {
          "timestamp": timestamp,
          "deviceCode": deviceCode,
          "ver": "v1.0",
          "sign": sign
        },
        "data": {},
        "offset": 0,
        "pageSize": 10
      };
      console.log('接口地址:', url);
      console.log('接口传参:', JSON.stringify(params));
      call.postData(url, params,
        res => {
          console.log('获取食谱成功', res);
          that.setData({
            recipeList: res.data||[]
          })
        },
        err => {
          console.log('获取食谱失败', err)
        })
    },
    _searchRecipe() {
      var that = this;
      const {
        timestamp,
        deviceCode,
        sign,
        foodObj
      } = that.data
      const url = `/service/app/v1/recipe/searchRecipe`;
      const params = {
        "header": {
          "timestamp": timestamp,
          "deviceCode": deviceCode,
          "ver": "v1.0",
          "sign": sign
        },
        "data": foodObj,
        "offset": 0,
        "pageSize": 10
      };
      call.postData(url, params,
        res => {
          console.log('获取指定食谱成功', res);
          that.setData({
            recipeList: res.data||[]
          })
        },
        err => {
          console.log('获取指定食谱失败', err)
        })

    }
  }
})